package com.apchernykh.types;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.os.Parcel;
import android.os.Parcelable;

import com.apchernykh.rozhlasapp.utils.ConditionalLog;

public class OnGoingInfoURL implements Parcelable {
	public String mNow = null;
	public String mNext = null;

	
	public static final Parcelable.Creator<OnGoingInfoURL> CREATOR = new Parcelable.Creator<OnGoingInfoURL>() {
		public OnGoingInfoURL createFromParcel(Parcel iParcel) {
			return new OnGoingInfoURL(iParcel);
		}

		public OnGoingInfoURL[] newArray(int iSize) {
			return new OnGoingInfoURL[iSize];
		}
	};

	OnGoingInfoURL(String iNow, String iNext) {
		mNow = iNow;
		mNext = iNext;
	}

	public OnGoingInfoURL() {
		// TODO Auto-generated constructor stub
	}

	public OnGoingInfoURL(Parcel iParcel) {
		mNow = iParcel.readString();
		mNext = iParcel.readString();
	}
	
	@Override
	public void writeToParcel(Parcel oParcel, int iUnusedFlags) {
		oParcel.writeString(mNow);
		oParcel.writeString(mNext);
	}

	public void refresh(Node aNode) {
		try {
			mNow = getAttr(aNode, "now", "url");
			mNext = getAttr(aNode, "next", "url");
		} catch (Exception e) {
			ConditionalLog
					.d("error parsing program/now|next tag", e.toString());
		}
	}

	private String getAttr(Node aNode, String aElementName, String aAttrName) {
		if (aNode == null)
			return null;
		NodeList aNodeList = ((Element) aNode).getElementsByTagName(aElementName);
		if (aNodeList == null || aNodeList.getLength() == 0)
			return null;
		Element b = (Element) aNodeList.item(0);
		if (b == null)
			return null;
		return b.getAttribute(aAttrName);
	}

	public void copy(OnGoingInfoURL iOnGoingUrl) {
		mNow = iOnGoingUrl.mNow;
		mNext = iOnGoingUrl.mNext;
	}
	
	@Override
	public int describeContents() {
		return 0;
	}



}
